package com.cynteka.cyfoman.ui;

/**

 * User: Sergey
 * Date: 27.07.11
 * Time: 14:14
 */

import org.apache.commons.chain.Catalog;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.impl.ContextBase;
import org.jdesktop.swingx.JXTable;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class StrippedTable extends JXTable {

    private Catalog catalog;

    public StrippedTable() {
    }

    public StrippedTable(TableModel dm) {
        super(dm);
    }

    public StrippedTable(String[][] data, String[] fields) {
        super(data, fields);
        setFillsViewportHeight(true); //to show the empty space of the table
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        paintEmptyRows(g);
    }


    public void paintEmptyRows(Graphics g) {
        Graphics newGraphics = g.create();
        newGraphics.setColor(UIManager.getColor("Table.gridColor"));

        Rectangle rectOfLastRow = getCellRect(getRowCount() - 1, 0, true);
        int firstNonExistentRowY = rectOfLastRow.y; //the top Y-coordinate of the first empty tablerow

        if (getVisibleRect().height > firstNonExistentRowY) //only paint the grid if empty space is visible
        {
            //fill the rows alternating and paint the row-lines:
            int rowYToDraw = (firstNonExistentRowY - 1) + getRowHeight(); //minus 1 otherwise the first empty row is one pixel to high
            int actualRow = getRowCount() - 1; //to continue the stripes from the area with table-data

            while (rowYToDraw < getHeight()) {
                if (actualRow % 2 == 0) {
                    newGraphics.setColor(color2); //change this to another color (Color.YELLOW, anyone?) to show that only the free space is painted
                    newGraphics.fillRect(0, rowYToDraw, getWidth(), getRowHeight());
                    newGraphics.setColor(UIManager.getColor("Table.gridColor"));
                }

                newGraphics.drawLine(0, rowYToDraw, getWidth(), rowYToDraw);

                rowYToDraw += getRowHeight();
                actualRow++;
            }


            //paint the column-lines:
            int x = 0;
            for (int i = 0; i < getColumnCount(); i++) {
                TableColumn column = getColumnModel().getColumn(i);
                x += column.getWidth(); //add the column width to the x-coordinate

                newGraphics.drawLine(x - 1, firstNonExistentRowY, x - 1, getHeight());
            }

            newGraphics.dispose();

        } //if empty space is visible

    } //paintEmptyRows

    private Color color2 = new Color(240, 240, 240);

    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);

        if (!isRowSelected(row)) {
            c.setBackground(getBgColor(row, column));
        }

        Color color = getColor(row, column);
        if(color != null)
            c.setForeground(color);

        Font font = getFont(row, column);
        if(font != null)
            c.setFont(font);

        return c;
    }

    protected Color getBgColor(int row, int column) {
        return row % 2 == 0 ? getBackground() : color2;
    }

    protected Color getColor(int row, int column) {
        return Color.BLACK;
    }

    protected Font getFont(int row, int column) {
        return null;
    }

    public static void main(String[] argv) {
        String data[][] = {{"A0", "B0", "C0"}, {"A1", "B1", "C1"}, {"A2", "B2", "C2"}, {"A3", "B3", "C3"}, {"A4", "B4", "C4"}};
        String fields[] = {"A", "B", "C"};

        JFrame frame = new JFrame("a JTable with striped empty space");
        StrippedTable table = new StrippedTable(data, fields);
        JScrollPane pane = new JScrollPane(table);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(pane);
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    @Override
    public void setModel(TableModel dataModel) {
        super.setModel(dataModel);    //To change body of overridden methods use File | Settings | File Templates.
    }
}
